﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CocisModel;
using CocisDBUtility;

namespace CocisDAL
{
    public class RoleDAL
    {
        #region 判断角色是否存在
        /// <summary>
        /// 判断角色是否存在
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public int CheckRole(string names)
        {
            string sql = "select roleId from Roles where roleName='" + names + "'";
            int i = 0;
            SqlDataReader sdr = SQLHelper.ExecuteReader(SQLHelper.ConStr, CommandType.Text, sql, null);
            if (sdr.Read())
            {
                i = 1;

            }
            return i;
        }
        #endregion


        #region 获取所有角色信息
        /// <summary>
        /// 获取所有角色信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<RoleInfo> GetAllRole(int parentId)
        {
            string sql = "select * from Roles where ParentId='" + parentId + "'";
            using (SqlDataReader read = SQLHelper.ExecuteReader(SQLHelper.ConStr, System.Data.CommandType.Text, sql, null))
            {
                List<RoleInfo> list = new List<RoleInfo>();
                while (read.Read())
                {
                    RoleInfo s = new RoleInfo();

                    s.RoleId = read.GetInt32(0);
                    s.RoleName = read.GetString(1);
                   
                    list.Add(s);
                }
                return list;
            }
        }

        #endregion

        #region 获取所有角色信息
        /// <summary>
        /// 获取所有角色信息--返回数据集
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllRoleDataSet()
        {
            string sql = "select * from rolesInfo";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);

        }

        #endregion

        #region 查询角色信息---通过角色编号
        /// <summary>
        /// 查询角色信息---通过角色编号
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public DataSet GetRoleInfoByRoleId(int roleId)
        {
            string sql = "select * from RolesInfo where roleId='" + roleId + "'";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 新增角色信息
        /// <summary>
        /// 新增角色信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public int AddRole(string roleName,string roleDesc)
        {
            string sql = "insert into rolesInfo(roleName,roleDesc) values('" + roleName + "','" + roleDesc + "')";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 修改角色信息
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="roleName"></param>
        /// <param name="roleDesc"></param>
        /// <returns></returns>
        public int UpdateRole(int roleId, string roleName,string roleDesc)
        {
            string sql = "update rolesInfo set roleName='" + roleName + "',roleDesc='" + roleDesc + "' where roleId='" + roleId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);

        }
        #endregion

        #region 删除角色信息
        public int DeleteRole(int roleId)
        {
            string sql = "delete from rolesInfo where roleId='" + roleId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);


        }

        #endregion
    }
}
